package com.qf.index.gcrdo;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.github.houbb.heaven.util.lang.StringUtil;
import com.qf.index.TransApi;
import com.qf.index.Utils;
import lombok.SneakyThrows;

import java.sql.SQLException;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Author: 葛成瑞
 * @Date: 2025/4/22 13:45
 */
public class GetNoTransDoIt {
    @SneakyThrows
    public static void main(String[] args) {
        // 进行翻译
        String table = "unified_search_no_trans";
        String saveTable = "unified_search_no_trans";
        Db use = Db.use();
        Integer maxId =use.query("select max(id) as maxid from "+table).get(0).getInt("maxid");
        System.out.println(maxId);
        Integer minId = 0;
        Integer step = 10000;
        ThreadPoolExecutor executor = new ThreadPoolExecutor(30, 100, 0L, java.util.concurrent.TimeUnit.MILLISECONDS,
                new java.util.concurrent.ArrayBlockingQueue<>(10000), new ThreadPoolExecutor.CallerRunsPolicy());

        // 删除无用的 executor.submit()
        for (int i = minId; i < maxId; i+=step) {
            final int currentStart = i;
            executor.submit(() -> { // 将每个批次包装为独立任务
//                System.out.println("页码："+currentStart+" 到 "+(currentStart+step)+" 开始查询");
                String sql = "select id,title,cn_ti from "+table+" where id > ? and id <= ?";
                try {
                    var list = use.query(sql, currentStart, currentStart+step);
                    for (Entity entity : list) {
                        String title = entity.getStr("title");
                        String cn_ti = entity.getStr("cn_ti");
                        if (StringUtil.isNotBlank(title)&&StringUtil.isEmpty(cn_ti)){
                            boolean isCn = Utils.isContainChinese(title);
                            String trans = trans(title, isCn);
                            if (StringUtil.isNotBlank(trans)){
                                entity.set("cn_ti",trans);
                                entity.setTableName(saveTable);
                                use.insertOrUpdate(entity,"id");
                            }
                        }
                    }
                } catch (SQLException e) {
                    System.err.println("数据库操作异常：" + e.getMessage());
                }
            });
        }
        executor.shutdown();
        while (!executor.isTerminated()) {
            // 等待所有任务完成

            int activeCount = executor.getActiveCount();
            System.out.println("当前活动线程数：" + activeCount);
            long completedTaskCount = executor.getCompletedTaskCount();
            System.out.println("已完成任务数：" + completedTaskCount);
            long taskCount = executor.getTaskCount();
            System.out.println("总任务数：" + taskCount);
            Thread.sleep(10000);
        }
        System.out.println("所有任务已完成");
    }

    public static String trans(String str,boolean isCn) {
        String res = "";
        if (!isCn){
            String s = TransApi.youDaoEnToCnTrans(str);
            if (StringUtil.isNotBlank(s)&&!s.contains("502 Bad Gateway")){
                res = s;
            }else {
                for (int i = 0; i < 3; i++) {
                    s = TransApi.youDaoEnToCnTrans(str);
                    if (StringUtil.isNotBlank(s) && !s.contains("502 Bad Gateway")) {
                        res = s;
                        break;
                    }

                }
            }

        }
        return res;
    }

}
